package com.ddwl.message.service;

import com.ddwl.common.exception.CommonException;
import com.ddwl.common.exception.ExceptionCode;
import com.ddwl.common.util.ObjectUtil;
import com.ddwl.common.util.UUIDGenerator;
import com.ddwl.message.mapper.NoticeMapper;
import com.ddwl.message.model.Notice;
import com.ddwl.schema.bo.base.PageBo;
import com.ddwl.schema.bo.message.NoticeBo;
import com.ddwl.schema.bo.message.NoticeSearchBo;
import com.ddwl.schema.vo.base.PageVo;
import com.ddwl.schema.vo.message.NoticeVo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class NoticeService {

    @Autowired
    private NoticeMapper noticeMapper;
  //  @Autowired
 //   private ShopGrpcClient shopGrpcClient;

    /**
     * 客户端通知
     *
     * @param pageBo
     * @return
     */
    public List<NoticeVo> index(PageBo<NoticeSearchBo> pageBo) {
        PageHelper.startPage(pageBo.getPage(), pageBo.getPageSize()).count(false);
        List<Notice> entities = noticeMapper.findAll(pageBo.getParam());
        return entities.stream().map(entity -> {
            NoticeVo vo = new NoticeVo();
            BeanUtils.copyProperties(entity, vo);
            return vo;
        }).collect(Collectors.toList());
    }

    public PageVo<NoticeVo> page(PageBo<NoticeSearchBo> pageBo) {
        PageVo<NoticeVo> pageVo = new PageVo<>();
        pageBo.setPage(pageBo.getPage());
        pageVo.setSize(pageBo.getPageSize());
        PageHelper.startPage(pageBo.getPage(), pageBo.getPageSize());
        Page<Notice> pageable = (Page<Notice>) noticeMapper.findAll(pageBo.getParam());
        pageVo.setTotal(pageable.getTotal());
        List<Notice> entities = pageable.getResult();
        Set<String> mchIds = new HashSet<>();
        entities.forEach(entity -> {
            if (StringUtils.hasText(entity.getMchId())) {
                mchIds.add(entity.getMchId());
            }
        });
        Map<String, String> mchMap = new HashMap<>();
        if (!mchIds.isEmpty()) {
           /* Iterator<MchReply> iterator = shopGrpcClient.getMerchants(mchIds.stream().distinct().collect(Collectors.toList()));
            while (iterator.hasNext()) {
                MchReply reply = iterator.next();
                mchMap.put(reply.getId(), reply.getName());
            }*/
        }
        pageVo.setData(entities.stream().map(entity -> {
            NoticeVo vo = new NoticeVo();
            BeanUtils.copyProperties(entity, vo);
            vo.setMchName(mchMap.get(entity.getMchId()));
            return vo;
        }).collect(Collectors.toList()));
        BeanUtils.copyProperties(pageBo, pageVo);
        return pageVo;
    }

    @Transactional(rollbackFor = Throwable.class)
    public String create(NoticeBo bo) {
        String id = UUIDGenerator.getShortUUID();
        Notice entity = new Notice();
        BeanUtils.copyProperties(bo, entity);
        entity.setId(id);
        noticeMapper.insert(entity);
        return id;
    }

    @Transactional(rollbackFor = Throwable.class)
    public void update(NoticeBo bo) {
        Notice entity = noticeMapper.findByPk(bo.getId());
        if (entity == null) {
            throw new CommonException(ExceptionCode.DATA_NOT_EXISTS);
        }
        BeanUtils.copyProperties(bo, entity, ObjectUtil.getNullFields(bo));
        noticeMapper.update(entity);
    }

    @Transactional(rollbackFor = Throwable.class)
    public void delete(String id) {
        noticeMapper.delete(id);
    }

    public NoticeVo detail(String id) {
        Notice entity = noticeMapper.findByPk(id);
        if (entity == null) {
            throw new CommonException(ExceptionCode.DATA_NOT_EXISTS);
        }
        NoticeVo vo = new NoticeVo();
        BeanUtils.copyProperties(entity, vo);
        return vo;
    }
}
